package club.banyuan.oop.UserDemo;

/**
 * @author edz
 * @version 1.0
 * @date 2020/11/18 上午11:22
 */
public class ProductService {
    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    private Product[] products = new Product[10];
    private String[] address = {"江苏南京", "江苏泰州", "贵州贵阳", "江苏扬州", "湖南长沙", "台湾台北"};

    {
        for (int i = 0; i < products.length; i++) {
            Product product = new Product();
            product.setProductId("9527" + i);
            product.setPrice((int) (Math.random() * 50) + 50);
            product.setProName("产品" + (i + 1) + "号");
            product.setSize(("" + (i + 1)));

            int index = (int) (Math.random() * address.length);
            product.setProAddress(address[index]);
            products[i] = product;
        }
    }

    //商品详情列表
    public void showAll() {
        for (int i = 0; i < products.length; i++) {
            System.out.println(products[i]);
        }
    }

    //购买产品
    public void buy(String productId, User user) {
        Product product = getById(productId);
        if (product != null) {
            //将产品塞给用户
            userService.addProduct(product, user);
        }
    }

    //写一个方法用来判断是否存在该产品
    public Product getById(String productId) {
        for (int i = 0; i < products.length; i++) {
            if (products[i].getProductId().equals(productId)) {
                return products[i];
            }
        }
        return null;
    }
}
